package com.ruoyi.system.controller;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.chrono.ChronoLocalDate;
import java.util.Date;
import java.util.List;
import java.io.IOException;
import java.util.concurrent.TimeUnit;
import javax.servlet.http.HttpServletResponse;

import cn.hutool.core.date.DateTime;
import cn.hutool.core.lang.Snowflake;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.system.domain.Opts;
import com.ruoyi.system.domain.Users;
import com.ruoyi.system.task.Myscheduler;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.PreAuthorize;
import com.ruoyi.system.domain.TbRoomReservation;
import com.ruoyi.system.service.ITbRoomReservationService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 预约房源Controller
 * 
 * @author wish
 * @date 2021-06-18
 */
@RestController
@RequestMapping("/reservation")
public class TbRoomReservationController extends BaseController
{
    @Autowired
    private ITbRoomReservationService tbRoomReservationService;
    @Autowired
    Myscheduler myscheduler;
    @Autowired
    RedisTemplate redisTemplate;
    /**
     * 查询预约房源列表
     */
    @PreAuthorize(hasPermi = "system:reservation:list")
    @GetMapping("/list")
    public TableDataInfo list(TbRoomReservation tbRoomReservation)
    {
        startPage();
        List<TbRoomReservation> list = tbRoomReservationService.selectTbRoomReservationList(tbRoomReservation);
        return getDataTable(list);
    }

    @PreAuthorize(hasPermi = "system:reservation:selectRegionProvince")
    @GetMapping("selectRegionProvince")
    public TableDataInfo changeProvince(@RequestParam("provinceId") int provinceId){
        List<TbRoomReservation> list = tbRoomReservationService.selectRegionProvince(provinceId);
        return getDataTable(list);
    }

    @PreAuthorize(hasPermi = "system:reservation:selectRegionCount")
    @GetMapping("selectRegionCount")
    public TableDataInfo selectRegionCount(@RequestParam("countId") int countId){
        List<TbRoomReservation> list = tbRoomReservationService.selectRegionCount(countId);
        return getDataTable(list);
    }

    @PreAuthorize(hasPermi = "system:reservation:changeCity")
    @GetMapping("changeCity")
    public TableDataInfo changeCity(){
       List<TbRoomReservation> list = tbRoomReservationService.selectRegionCity();
       return getDataTable(list);
    }

    @GetMapping("getRoomOpts")
    public R getRoomOpts(){
        List<Opts> list = tbRoomReservationService.getRoomOpts();
        return R.ok(list);
    }

    @PostMapping("addReservations")
    public int addReservation(@RequestBody TbRoomReservation tbRoomReservation) throws SchedulerException, InterruptedException {
        int i = 0;
        Snowflake snowflake = new Snowflake(1,1);
        String s = snowflake.nextIdStr();
        tbRoomReservation.setId(s);
        Boolean b = redisTemplate.opsForValue().setIfAbsent(tbRoomReservation.toString(), "addReservation", 5, TimeUnit.MINUTES);
        if(b){
            i = tbRoomReservationService.addReservation(tbRoomReservation);
            LocalDateTime reserveTime = tbRoomReservation.getReserveTime();
            if(reserveTime.isBefore(LocalDateTime.now())){
                //开始执行定时任务
                myscheduler.GenMyscheduler();
                myscheduler.genJob();
                myscheduler.ScheduleJobStart();
                //线程休眠0.5s   执行一次job后关闭定时任务
                Thread.sleep(500);
                //关闭定时任务
                 myscheduler.ScheduleJobSleep();
            }

        }

        return i;
    }

}
